import random

class pyDemin:
    
    def __init__(self,largeur,longueur,nbMines):
        self.largeur=largeur
        self.longueur=longueur
        self.nbMines=nbMines
        self.grilleMine=[-1]*nbMines+[0]*(largeur*longueur-nbMines)
        self.grilleDec=['c']*(largeur*longueur)
        random.shuffle(self.grilleMine)

    def get_value_Dec(self,x,y):
        return self.grilleDec[x+y*self.largeur]

    def set_value_Dec(self,x,y,nb):
        self.grilleDec[x+y*self.largeur]=nb

    def get_value_GMine(self,x,y):
        return self.grilleMine[x+y*self.largeur]

    def set_value_GMine(self,x,y,nb):
        self.grilleMine[x+y*self.largeur]=nb

    def __repr__(self):
        l=''
        for i in range(self.largeur):
            l+='---'
        l+='\n'
        for j in range(self.longueur):
            for i in range(self.largeur):
                n=self.get_value_Dec(i,j)
                if n=='c':
                    l+='|  '
                elif n=='d':
                    l+='|x '
                else:
                    l+='|%d '%n
            l+='|\n'
            for i in range(self.largeur):
                l+='---'
            l+='\n'
            
        l+='\n'
        for i in range(self.largeur):
            l+='---'
        l+='\n'
        for j in range(self.longueur):
            for i in range(self.largeur):
                n=self.get_value_GMine(i,j)
                if n==0:
                    l+='|  '
                elif n>0:
                    l+='|%d '%n
                else:
                    l+='|%d'%n
            l+='|\n'
            for i in range(self.largeur):
                l+='---'
            l+='\n'
        
        return l
        

    def nbMinesVoisinage(self):
        n=0
        cpt=0
        for i in range(self.largeur):
            for j in range(self.longueur):
                cpt=0
                for k in range(-1,2,1):
                    for l in range(-1,2,1):
                        if i+k>=0 and i+k<self.largeur and j+l>=0 and j+l<self.longueur:
                            n=self.get_value_GMine(i+k,j+l)
                            if n==-1:
                                cpt+=1
                if self.get_value_GMine(i,j)!=-1:
                    self.set_value_GMine(i,j,cpt)
        
    def decCasePos(self,x,y):
        n=self.get_value_Dec(x,y)
        if n=='c':
            m=self.get_value_GMine(x,y)
            if m==-1:
                return -1
            else:
                self.set_value_Dec(x,y,m)
                return m
        else:
            return n

    def decCaseVoisinage(self,x,y):
        l=[]
        m=self.decCasePos(x,y)
        if m==0:
            self.set_value_Dec(x,y,'d')
            for i in range(-1,2):
                for j in range(-1,2):
                    if x+i>=0 and x+i<self.largeur and y+j>=0 and y+j<self.longueur:
                        n=self.decCasePos(x+i,y+j)
                        if n==0:
                            l.append([x+i,y+j])
                        if n==-1:
                            print "GAME OVER"
        return l

    def decCase(self,x,y):
        l=self.decCaseVoisinage(x,y)
        for i in range(len(l)):
            nx=l[i][0]
            ny=l[i][1]
            if self.get_value_Dec(nx,ny)=='c':
                self.decCase(nx,ny)


    def deched(self,x,y):
        self.set_value_Dec(x,y,'d')
        m = self.get_value_GMine(x,y)
        if m==-1:
            return -1
        if m>0:
            self.set_value_Dec(x,y,m)
            return 0
        for i in range(-1,2):
            for j in range(-1,2):
                nx = x+i
                ny = y+j
                if nx>=0 and nx<self.largeur and ny>=0 and ny<self.longueur:
                    if self.get_value_Dec(nx,ny)=='c':
                        self.deched(nx,ny)
        return self.testFin()

    def testFin(self):
        cp=0
        for j in range(self.longueur):
            for i in range(self.largeur):
                k=self.get_value_GMine(i,j)
                if k!='c':
                    cp+=1
        n=self.longueur*self.largeur-self.nbMines
        if cp==n:
            return -2
        else:
            return 0

#random.seed(1)
#py=pyDemin(10,5,9)
#print py
#py.nbMinesVoisinage()
#py.deched(3,0)
#print py  
            
